{# **
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/OSL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to https://devdocs.prestashop.com/ for more information.
 *
 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 * #}

{% block entity_search_input_widget %}
  {# We cannot use the escape filter for remote_url or it breaks the url and it is not callable any more #}
  {%- set attr = attr|merge({
    'data-prototype-template': form_row(prototype),
    'data-prototype-index': prototype.vars.name|e('html_attr'),
    'data-prototype-mapping': prototype_mapping|json_encode,
    'data-identifier-field': identifier_field,
    'data-filtered-identities': filtered_identities|json_encode,
    'data-remove-modal': remove_modal|json_encode,
    'data-remote-url': remote_url,
    'data-data-limit': limit,
    'data-min-length': min_length,
    'data-allow-delete': form.vars.allow_delete ? 1 : 0,
    'data-suggestion-field': form.vars.suggestion_field
  }) -%}
  {%- set attr = attr|merge({class: (attr.class|default('') ~ ' entity-search-widget')|trim}) -%}

  {# We use widget_container_attributes in this widget because we do not want the name property #}
  <div {{ block('widget_container_attributes') }}>
    {# If dynamic addition is disabled we don't display the search input field #}
    {% if form.vars.allow_search %}
      <div class="search search-with-icon">
        {%- set attr = search_attr|merge({
          class: (search_attr.class|default('') ~ ' entity-search-input form-control')|trim,
          autocomplete: 'off',
          placeholder: placeholder,
          type: 'text'
        }) -%}
        {%- set id = form.vars.id ~ '_search_input' -%}
        <input {{ block('widget_container_attributes') }} />
      </div>
    {% endif %}
    {{- block('form_help') -}}

    {%- set id = form.vars.id ~ '_list' -%}
    {% if list_layout == 'table' %}
      {{- block('entity_search_table_layout') -}}
    {% else %}
      {{- block('entity_search_list_layout') -}}
    {% endif %}
    {% if empty_state is not null %}
      <div class="alert alert-info empty-entity-list mt-2" role="alert">
        <p class="alert-text">
          {{ empty_state|raw('html') }}
        </p>
      </div>
    {% endif %}
  </div>
{% endblock %}

{% block entity_search_list_layout %}
  {%- set attr = list_attr|merge({class: (list_attr.class|default('') ~ ' entities-list entities-list-container')|trim}) -%}
  <ul {{ block('widget_container_attributes') }}>
    {{- block('form_rows') -}}
  </ul>
{% endblock %}

{% block entity_search_table_layout %}
  {%- set attr = list_attr|merge({class: (list_attr.class|default('') ~ ' entities-list-container')|trim}) -%}
  <div {{ block('widget_container_attributes') }}>
    <div class="row">
      <div class="col-sm">
        <table class="table">
          <thead class="thead-default">
            <tr>
            {% for child in prototype.children %}
              {% set childType = child.vars.block_prefixes.1 %}
              {% if childType != 'hidden' %}
                <th>{{ child.vars.label }}</th>
              {% endif %}
            {% endfor %}

            {# If item deletion is disabled we don't display the extra column for actions #}
            {% if form.vars.allow_delete %}
              <th></th>
            {% endif %}
            </tr>
          </thead>
          <tbody class="entities-list">
          {{- block('form_rows') -}}
          </tbody>
        </table>
      </div>
    </div>
  </div>
{% endblock %}

{% block entity_item_row %}
  {% if form.parent.vars.list_layout == 'table' %}
    {{- block('entity_item_table_row') -}}
  {% else %}
    {{- block('entity_item_list_row') -}}
  {% endif %}
{% endblock %}

{% block entity_item_list_row %}
  {% set attr = attr|merge({class: (attr.class|default('') ~ ' media entity-item')|trim}) %}
  <li {{ block('widget_container_attributes') }}>
    <div class="media-left">
      {{ form_widget(form.image) }}
    </div>
    <div class="media-body media-middle">
      {{ form_widget(form.name) }}
      <i class="material-icons entity-item-delete">clear</i>
    </div>
    {{ form_rest(form) }}
  </li>
{% endblock %}

{%- block entity_item_table_row -%}
  {% set attr = attr|merge({class: (attr.class|default('') ~ ' entity-item')|trim}) %}
  <tr {{ block('widget_container_attributes') }}>
  {% for child in form.children %}
    {% set childType = child.vars.block_prefixes.1 %}
    {% if childType == 'hidden' %}
      {{ form_widget(child) }}
    {% else %}
    <td>
      {{ form_widget(child) }}
    </td>
    {% endif %}
  {% endfor %}

  {# If item deletion is disabled we don't display the extra column for actions #}
  {% if form.parent.vars.allow_delete %}
    <td>
      <i class="material-icons entity-item-delete">clear</i>
    </td>
  {% endif %}
  </tr>
{%- endblock -%}

{% block searched_customer_row %}
  {% set attr = attr|merge({class: (attr.class|default('') ~ ' media entity-item')|trim}) %}
  <li {{ block('widget_container_attributes') }}>
    <div class="media-body media-middle">
      {{ form_widget(form.fullname_and_email) }}
      <i class="material-icons entity-item-delete">clear</i>
    </div>
    {{ form_widget(form.id_customer) }}
  </li>
{% endblock %}
